package com.worklog.controller;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.worklog.model.Role;
import com.worklog.model.RoleResources;
import com.worklog.service.RoleResourcesService;
import com.worklog.service.RoleService;
import com.worklog.util.ResultData;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {
	@Resource
	private RoleService roleService;
	@Resource
	private RoleResourcesService roleResourcesService;

	@RequestMapping
	public Map<String, Object> getAll(Role role, String draw,
			@RequestParam(required = false, defaultValue = "1") int start,
			@RequestParam(required = false, defaultValue = "10") int length) {

		Map<String, Object> map = new HashMap<>();
		PageInfo<Role> pageInfo = roleService.selectByPage(role, start, length);
		map.put("draw", draw);
		map.put("recordsTotal", pageInfo.getTotal());
		map.put("recordsFiltered", pageInfo.getTotal());
		map.put("data", pageInfo.getList());
		return map;
	}

	@RequestMapping("/rolesWithSelected")
	public List<Role> rolesWithSelected(Integer uid) {
		return roleService.queryRoleListWithSelected(uid);
	}

	// 分配角色
	@RequestMapping("/saveRoleResources")
	public String saveRoleResources(RoleResources roleResources) {
		if (StringUtils.isEmpty(roleResources.getRoleid()))
			return "error";
		try {
			roleResourcesService.addRoleResources(roleResources);
			return "success";
		} catch (Exception e) {
			return "fail";
		}
	}

	@RequestMapping(value = "/add")
	public ResultData add(@RequestBody Role role) {
		ResultData result = new ResultData();
		try {
			Role u = roleService.selectByRolename(role.getRoledesc());
			if (u != null) {
				result.error("角色名已存在!", null);
				return result;
			}
			role.setCreateTime(new Timestamp(System.currentTimeMillis()));
			role.setUpdateTime(new Timestamp(System.currentTimeMillis()));

			roleService.save(role);
			result.success("新增角色成功");
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			result.error("新增角色失败");
		}
		return result;
	}

	@RequestMapping(value = "/delete")
	public ResultData delete(Integer id) {
		ResultData result = new ResultData();
		try {
			if (id == 1) {
				result.error("该角色不可删除");
				return result;
			}
			roleService.delRole(id);
			result.success("删除成功");
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			result.error("删除失败");
		}
		return result;
	}

	@RequestMapping(value = "/update")
	public ResultData update(@RequestBody Role role) {
		ResultData result = new ResultData();
		try {
			Integer roleId = role.getId();
			Role u = roleService.selectByRolename(role.getRoledesc());
			if (u != null && !roleId.equals(u.getId())) {
				result.error("角色名已存在!", null);
				return result;
			}
			role.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			roleService.updateNotNull(role);
			result.success("修改成功");
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			result.error("修改失败");
		}
		return result;
	}

	@RequestMapping(value = "/deleteRole")
	public ResultData deleteUser(@RequestBody Integer[] ids) {
		ResultData result = new ResultData();
		List<Integer> idList = Arrays.asList(ids);
		try {
			if (idList.contains(1)) {
				result.error("该角色不可删除");
				return result;
			}
			roleService.delRoles(idList);
			result.success("删除成功");
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			result.error("删除失败");
		}
		return result;
	}

	@RequestMapping(value = "/queryAllRole")
	public ResultData queryAllRole() {
		ResultData result = new ResultData();
		try {
			List<Role> list = roleService.queryAllRole();
			result.setData(list);
			result.setTotal(list.size());
			result.success("查询所有角色成功！");
		} catch (Exception e) {
			log.info(e.getMessage(), e);
			result.error("查询所有角色失败！");
		}
		return result;
	}

}
